<?php 
try {
	include_once('db/constants.php');
	include_once('db/get_categories_db.php');
	include_once('db/error_log_db.php');
	include_once('facebook_login.php');

	$current_file = "publishedCategories";
	include_once("users_flow_logger.php");

	define("MAX_PAGES_OPTIONS", "5");
	define("CONTENTS_SHOWN_PER_PAGE", "8");
	define("MAX_CONTENT_HEIGTH", "62");
	
	
	$user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : $user_profile['id'];
	$name = isset($_REQUEST['first_name']) ? $_REQUEST['first_name'].' '.$_REQUEST['last_name'] : $user_profile['first_name'].' '.$user_profile['last_name'];
	
	$categories_json = null;
	if (isset($_REQUEST['user_id'])) {
		$categories_json = get_user_category_db(CONTENTS_SHOWN_PER_PAGE, -1, $user_id, "1");
	} else {
		$categories_json = get_user_category_db(CONTENTS_SHOWN_PER_PAGE, -1, $user_id, -1);
	}
	$categories = json_decode( $categories_json );
	
	$number_pages = ceil( get_amount_categories($user_id) / CONTENTS_SHOWN_PER_PAGE );

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title><?php echo HTML_TITLE;?></title>
	<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
	<!--[if lte IE 6]><link rel="stylesheet" href="css/ie6.css" type="text/css" media="all" /><![endif]-->
	
	<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
	<script src="js/spin.js" type="text/javascript"></script>
	<script src="js/wrap_content.js?<?php echo REVISION;?>" type="text/javascript"></script>
	<script src="js/general_published_content_functions.js" type="text/javascript"></script>

	<script>
		MAX_PAGES_OPTIONS = <?php echo MAX_PAGES_OPTIONS; ?>;
		CONTENTS_SHOWN_PER_PAGE = <?php echo CONTENTS_SHOWN_PER_PAGE; ?>;
		MAX_CONTENT_HEIGTH = <?php echo MAX_CONTENT_HEIGTH; ?>;
		numberPages = <?php echo $number_pages; ?>;
		contents = JSON.parse( "<?php echo addslashes($categories_json); ?>" );
		lastIdContent = contents[ contents.length - 1 ].category_id;
		allContentsLoaded = (contents.length < CONTENTS_SHOWN_PER_PAGE) ? true : false;
		userId = <?php echo $user_id; ?>;
		getNotAnonymous = <?php echo (isset($_REQUEST['user_id'])) ? "1" : "-1";?>;
		getContentsUrlCall = "db/get_categories_db.php?user_id=<?php echo $user_id; ?>";

		function showContentAt(index, category) {
			var maxEditTime = <?php echo CATEGORY_MAX_EDIT_TIME;?>;
			var maxRemoveTime = <?php echo CATEGORY_MAX_REMOVE_TIME;?>;
			var POINTS = "<?php echo POINTS;?>";
			var userId = <?php echo $user_profile['id'];?>;

			document.getElementById('content-div-' + index).style.display = '';
			document.getElementById('hr-' + index).style.display = '';

			document.getElementById('rank-td-' + index).innerHTML = category.rank + '.';
			document.getElementById('content-td-' + index).innerHTML = removeSlashes(category.name);
			document.getElementById('content-td-' + index).href = '<?php echo CONTENTS_URL; ?>?category_id=' + category.category_id;
			document.getElementById('likes-td-' + index).innerHTML = category.likes;
			document.getElementById('dislikes-td-' + index).innerHTML = category.dislikes;
			document.getElementById('fraction-likes-td-' + index).innerHTML = category.fraction_likes*100 + '%';
			//document.getElementById('score-td-' + index).innerHTML = parseFloat(category.score).toFixed( <?php echo POINTS_DECIMALS; ?> ) + " " + POINTS;
			document.getElementById('score-td-' + index).innerHTML = '';
			document.getElementById('edit-button-' + index).href = 'uploadCategory.php?category_id=' + category.category_id;

			wrapContentToHeight('content-td-' + index, MAX_CONTENT_HEIGTH);

			var todaySeconds = (new Date(<?php date('Y-m-d H:i:s');?>)).getTime()/1000;
			var contentSeconds =  (new Date(category.creation_date)).getTime()/1000;

			document.getElementById('edit-button-' + index).style.visibility = (todaySeconds - contentSeconds < maxEditTime && category.user_id == userId) ? 'visible' : 'hidden';

			if (todaySeconds - contentSeconds < maxRemoveTime && category.user_id == userId) {
				document.getElementById('delete-button-' + index).style.visibility = 'visible';
				document.getElementById('delete-button-' + index).onclick = function() { removeCategory(category.category_id); }
			} else {
				document.getElementById('delete-button-' + index).style.visibility = 'hidden';
			}

			document.getElementById('share-button-' + index).onclick = function() {	postFeed(category.category_id); }
			
			if ( category.fraction_likes > 0.5 )
				document.getElementById('fraction-likes-td-' + index).className = 'points-good';
			else 
				document.getElementById('fraction-likes-td-' + index).className = 'points-bad';
		}

		function removeCategory(category_id) {
			if (deletingContent == true)
				return;

			var userId = <?php echo $user_profile['id']; ?>;
			deletingContent = true;

			spinner.spin(spinnerTarget);

			$.post("db/remove_category_db.php?category_id=" + category_id + "&user_id=" + userId, {
			}, function(response){

				window.location.href= "<?php echo PUBLISH_CATEGORY; ?>";  
				
			});
		}
		
	</script>
</head>
<body>

	<div id="fb-root"></div>
	<script>(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) return;
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/es_LA/all.js";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));</script>


    <script>		
	window.fbAsyncInit = function() {
	FB.init({
	  appId : '<?php echo APP_ID;?>',
	  xfbml : true,
	  version : 'v2.0'
	});
	};
	(function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/es_LA/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));
      
	function postFeed(category_id) {
		FB.ui({
		method: 'share',
		href: '<?php echo APP_CANVAS; ?><?php echo CONTENTS_URL; ?>?category_id=' + category_id,
		}, function(response){});
	}
      
	function removeSlashes(str) {
		return str.replace(/\\/g, '');
	}

    </script>

<?php 
$headerFirstOption = SEE_MY_PUBLICATIONS;
$headerSecondOption = CATEGORIES;
$headerThirdOption = PUBLISH;

$headerFirstUrl = PUBLISH_CONTENT."?coming_from=".$current_file;
$headerSecondUrl = CATEGORIES_URL."?coming_from=".$current_file;
$headerThirdUrl = UPLOAD_CONTENT."?coming_from=".$current_file;

$headerFirstClass = "button";
$headerSecondClass = "button-highlighted-green";
$headerThirdClass = "button-highlighted-red";

include_once("header_redirects.php"); 
include_once("header.php"); 
?>

<!-- Main -->
<div id="main">


	<div class="published-content-main-div">
		
		<!-- Cols -->
		<div style="height: 660px;">
			<div class="cl">&nbsp;</div>
			
			<!-- Col -->
			<div class="col" align="center" style="float: none; ">
				<h3><?php echo CATEGORIES_OF.$name ?></h3>

				<hr/>
				
				<?php
				
				if (count($categories) == 0) {
					echo "</br> </br>";
					echo "<h3> El usuario no tiene publicaciones. </h3>";
				}
				
				for ($i = 0; $i < CONTENTS_SHOWN_PER_PAGE; $i++) {
									
					echo '<div id="content-div-'.$i.'">';
						echo '<table class="best-content-page-table" border="0px" cellpadding="0px" cellspacing="0px">';
							echo '<tr>';
								echo '<td id="rank-td-'.$i.'" valign="top" class="ranking-number"></td>';
								echo '<td rowspan="3" valign="middle" class="published-content-content-td" > <a id="content-td-'.$i.'" class="no-link" ></a> </td>';
								echo '<td > <img src="css/images/happySmall.png" /> </td>';
								echo '<td id="likes-td-'.$i.'" class="points-good" width="70px" ></td>';
								
								echo '<td id="fraction-likes-td-'.$i.'" class="points-good" style="padding-left: 0px;"></td>';
								echo '<td rowspan="3" style="padding-left: 35px;"> <a id="share-button-'.$i.'" class="button" href="#">'.SHARE.'</a> </td>';
							echo '</tr>';
							echo '<tr>';
								echo '<td> <a id="delete-button-'.$i.'" class="delete-button" title="'.DELETE.'" href="#"></a> </td>';
								echo '<td style="padding: 3px;" ></td>';
								echo '<td></td>';
								echo '<td></td>';
							echo '</tr>';
							echo '<tr>';
								echo '<td> <a id="edit-button-'.$i.'" class="edit-button" title="'.EDIT.'"></a> </td>';
								echo '<td> <img src="css/images/sadSmall.png" />  </td>';
								echo '<td id="dislikes-td-'.$i.'" class="points-bad"></td>';
								echo '<td id="score-td-'.$i.'" class="points"></td>';
							echo '</tr>';
						echo '</table>';
					echo '</div>';
				
					echo '<hr id="hr-'.$i.'"/>';
				}
				?>
				
				<?php
				if ($number_pages > 1) {
					echo '<table border="0px" class="page-navigation-buttons">';
					echo '<tr>';
						echo '<td> <a class="previous-page-button" onclick="prevPage()" href="#"></a> </td>';
						echo '<td id="pages" class="pages"> ';
						echo '</td>';
						echo '<td> <a class="next-page-button" onclick="nextPage()" href="#"></a> </td>';
					echo '</tr>';
					echo '</table>';
				} ?>
				
			</div>
			<!-- End Col -->
					
		</div>
	</div>
</div>
<!-- End Main -->

<script>
	showPageContent();

	var spinnerTarget = document.getElementById('main');
	var spinner = new Spinner().spin(spinnerTarget);
	spinner.stop();
	
	showPageButtons();
		
</script>

</body>
</html>
